home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 January / macformat46.iso / Shareware Plus / Developers / Library / Grant's CGI Framework / Grant's CGI Framework / grantscgi / Util / VideoUtil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-20  |  4.5 KB  |  167 lines

  1. /*****
  2.  *
  3.  *    VideoUtil.c
  4.  *
  5.  *    This is a support file for "Grant's CGI Framework".
  6.  *    Please see the license agreement that accompanies the distribution package
  7.  *    for licensing details.
  8.  *
  9.  *    Copyright ©1996 by Grant Neufeld
  10.  *    grant@acm.com
  11.  *    http://arpp.carleton.ca/cgi/framework/
  12.  *
  13.  *****/
  14.  
  15. #include "MemoryUtil.h"
  16.  
  17. #include "VideoUtil.h"
  18.  
  19.  
  20. /***  FUNCTIONS  ***/
  21.  
  22. /* Tries to grab the current frame from a QuickCam (if one is installed)
  23.     and will save that image as a JPEG file in the file specified by theFileSpec.
  24.     
  25.     WARNING: This function has not been seriously tested and is probably not very
  26.         robust. Please exercise caution in using it.
  27.     
  28.     digitizerDepth specifies the bit-depth of the image. I recommend you use 32. */
  29. p_export OSErr
  30. SaveJPEGFromQuickCam ( FSSpec theFileSpec, short digitizerDepth )
  31. {
  32.     OSErr                    theErr;
  33.     PicHandle                thePict;
  34.     ComponentDescription    theQuickCamDesc;
  35.     Component                theQuickCam;
  36.     ComponentInstance        theQuickCamInstance;
  37.     Rect                    maxSrcRect;
  38.     CGrafPtr                theWorld;
  39.     CTabHandle                cTable;
  40.     PixMapHandle            thePMH;
  41.     short                    videoInputSource;
  42.     GWorldPtr                origGWorld;
  43.     GDHandle                origGDevice;
  44.     long                    imageDataSize;
  45.     SInt16                    theRefNum;
  46.     
  47.     long                    maxCompressedSize;
  48.     ImageDescriptionHandle    compressedImageDesc;
  49.     Ptr                        compressedImageData;
  50.  
  51.     thePict = (PicHandle)NewHandle( 0 );
  52.     if ( !thePict || MemError( ) )
  53.         return nil;
  54.  
  55.     //look for a color QuickCam
  56.     theQuickCamDesc.componentType                =    sgVideoDigitizerType;
  57.     theQuickCamDesc.componentSubType            =    'CQCm';
  58.     theQuickCamDesc.componentManufacturer        =    'Ctx7';
  59.     theQuickCamDesc.componentFlags                =    0;
  60.     theQuickCamDesc.componentFlagsMask            =    0;
  61.  
  62.     theQuickCam    =    FindNextComponent( 0, &theQuickCamDesc );
  63.     if ( !theQuickCam )    //look for the B&W
  64.     {
  65.         theQuickCamDesc.componentType                =    sgVideoDigitizerType;
  66.         theQuickCamDesc.componentSubType            =    'CtxV';
  67.         theQuickCamDesc.componentManufacturer        =    'Ctx6';
  68.         theQuickCamDesc.componentFlags                =    0;
  69.         theQuickCamDesc.componentFlagsMask            =    0;
  70.     }
  71.     if ( !theQuickCam )
  72.         return nil;
  73.  
  74.     theQuickCamInstance = OpenComponent( theQuickCam );
  75.     if ( !theQuickCamInstance )
  76.         return nil;
  77.  
  78.     theErr = VDGetInput( theQuickCamInstance, &videoInputSource );
  79.     if ( theErr )
  80.         return nil;
  81.  
  82.     theErr = VDGetMaxSrcRect( theQuickCamInstance, videoInputSource, &maxSrcRect );
  83.     if ( theErr )
  84.         return nil;
  85.  
  86.     theErr = VDSetDigitizerRect( theQuickCamInstance, &maxSrcRect );
  87.     if ( theErr )
  88.         return nil;
  89.  
  90.     cTable = digitizerDepth != 32 && digitizerDepth != 16 ? GetCTable( digitizerDepth ) : nil;
  91.     theErr = NewGWorld( &theWorld, digitizerDepth, &maxSrcRect, cTable, nil, keepLocal );
  92.     if ( cTable )
  93.         DisposeCTable( cTable );
  94.     if ( theErr )
  95.         return nil;
  96.  
  97.     thePMH = GetGWorldPixMap( theWorld );
  98.     LockPixels( thePMH );
  99.  
  100.     theErr = VDSetPlayThruDestination( theQuickCamInstance, thePMH, &maxSrcRect, nil, nil );
  101.     if ( theErr )
  102.         return nil;
  103.  
  104.     theErr = VDGrabOneFrame( theQuickCamInstance );
  105.     if ( theErr )
  106.         return nil;
  107.  
  108.     GetGWorld( &origGWorld, &origGDevice );
  109.     SetGWorld( theWorld, nil);        //get the pictures to save at the proper coordinates
  110.  
  111. //    thePict = OpenPicture( &maxSrcRect );
  112. //    HLock( (Handle)thePMH );
  113. //    CopyBits( (BitMapPtr)*thePMH, (BitMapPtr)*thePMH, &maxSrcRect, &maxSrcRect, srcCopy, nil );
  114. //    HUnlock( (Handle)thePMH );
  115. //    ClosePicture( );
  116.     
  117.     //•••••••••••••••••••••••••••••••••••••
  118.     
  119.     theErr = GetMaxCompressionSize ( thePMH, &maxSrcRect, 32, codecMinQuality, 'jpeg', anyCodec, &maxCompressedSize );
  120.     if ( theErr == noErr )
  121.     {
  122.         compressedImageData = MemoryNewPtr ( maxCompressedSize, &theErr );
  123.         if ( compressedImageData != NULL )
  124.         {
  125.             compressedImageDesc    = (ImageDescriptionHandle) MemoryNewHandle ( sizeof(ImageDescription), &theErr );
  126.             if ( compressedImageDesc != NULL )
  127.             {
  128.                 theErr = CompressImage ( thePMH, &maxSrcRect, codecMinQuality, 'jpeg', compressedImageDesc, compressedImageData );
  129.                 
  130.                 if ( theErr == noErr )
  131.                 {
  132.                     theErr = FSpDelete ( &theFileSpec );
  133.                     theErr = FSpCreate ( &theFileSpec, 'JVWR', 'JPEG', smSystemScript );
  134.                 }
  135.                 
  136.                 if ( theErr == noErr )
  137.                 {
  138.                     theErr = FSpOpenDF ( &theFileSpec, fsWrPerm, &theRefNum );
  139.                     if ( theErr == noErr )
  140.                     {
  141.                         imageDataSize = (*compressedImageDesc)->dataSize;
  142.                         theErr = FSWrite ( theRefNum, &imageDataSize, compressedImageData );
  143.                         
  144.                         FSClose ( theRefNum );
  145.                     }
  146.                 }
  147.             }
  148.         }
  149.     }
  150.     
  151.     //clean up
  152.     DisposeHandle    ( (Handle)compressedImageDesc );
  153.     DisposePtr        ( compressedImageData );
  154.     
  155.     
  156.     //•••••••••••
  157.     if ( theWorld )
  158.         DisposeGWorld( theWorld );
  159.  
  160.     SetGWorld( origGWorld, origGDevice );
  161.  
  162.     return theErr;
  163. } /* SaveJPEGFromQuickCam */
  164.  
  165.  
  166. /***  EOF  ***/
  167.